package cn.cloud.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static DateUtil instance = null;

	private DateUtil() {
	}

	public static DateUtil getInstance() {
		if (instance == null) {
			instance = new DateUtil();
		}
		return instance;
	}

	public String formatDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strdate = sdf.format(date);
		return strdate;
	}
	
	public String formatDate2(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String strdate = sdf.format(date);
		return strdate;
	}

	public Date parseDate(String date) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dd = sdf.parse(date);
		return dd;
	}

	public Date parseDate2(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dd;
		try {
			dd = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return dd;
	}

	/**
	 * @param d
	 * @return
	 */
	public boolean isEndOfMonth(Date d) {
		String s1 = dateToString(d, null);
		String s2 = dateToMonthEndString(d);
		if (s1.equals(s2)) {
			return true;
		}
		return false;
	}
	
	/**
	 * @param d
	 * @return
	 */
	public boolean isEndOfMonth(String str) {
		Date d=parseDate2(str);
		String s1 = dateToString(d, null);
		String s2 = dateToMonthEndString(d);
		if (s1.equals(s2)) {
			return true;
		}
		return false;
	}

	/**
	 * @param d
	 * @return
	 */
	public boolean isEndOfQuarter(Date d) {
		String s1 = dateToString(d, null);
		String s2 = "";
		String month = getMonthString(d);
		String year = getYearString(d);
		if (month.equals("03")) {
			s2 = "03-31";
		} else if (month.equals("06")) {
			s2 = "06-30";
		} else if (month.equals("09")) {
			s2 = "09-30";
		} else if (month.equals("12")) {
			s2 = "12-31";
		}
		s2 = year + "-" + s2;
		if (s2.equals(s1)) {
			return true;
		}
		return false;
	}

	/**
	 * @param d
	 * @return
	 */
	public boolean isEndOfYear(Date d) {
		String s1 = dateToString(d, null);
		String s2 = getYearString(d);
		if (s1.equals(s2 + "-12-31")) {
			return true;
		}
		return false;
	}

	/**
	 * @param d
	 * @return
	 */
	public boolean isEndOfHalfYear(Date d) {
		String s1 = dateToString(d, null);
		String s2 = getYearString(d);
		if (s1.equals(s2 + "-06-30")) {
			return true;
		}
		return false;
	}

	private String dateToString(Date date, String format) {
		String result = "";
		if (format == null || format.equals("")) {
			format = "yyyy-MM-dd";
		}
		try {
			if (date != null) {
				SimpleDateFormat dateFormat = new SimpleDateFormat(format);
				result = dateFormat.format(date);
			}
		} catch (Exception e) {
		}
		return result;
	}

	private String getMonthString(Date date) {
		String result = "";
		String format = "MM";
		try {
			if (date != null) {
				SimpleDateFormat dateFormat = new SimpleDateFormat(format);
				result = dateFormat.format(date);
			}
		} catch (Exception e) {
		}
		return result;
	}

	private String getYearString(Date date) {
		String result = "";
		String format = "yyyy";
		try {
			if (date != null) {
				SimpleDateFormat dateFormat = new SimpleDateFormat(format);
				result = dateFormat.format(date);
			}
		} catch (Exception e) {
		}
		return result;
	}

	private Date stringToDate(String srcDate, String pattern) {
		Date destDate = null;
		if (pattern == null || pattern.equals("")) {
			pattern = "yyyy-MM-dd";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			destDate = dateFormat.parse(srcDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return destDate;
	}

	private String stringToMonthBeginString(String strdate) {
		Date date = stringToDate(strdate, null);
		return dateToString(date, "yyyy-MM") + "-01";
	}

	private String stringToMonthEndString(String strdate) {
		Date date = stringToDate(stringToMonthBeginString(strdate), null);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return dateToString(calendar.getTime(), null);
	}

	private String dateToMonthEndString(Date date) {
		return stringToMonthEndString(dateToString(date, null));
	}
}
